/**
 * Created by Joseph on 2014/5/16.
 */
$(document).ready(function(){
    editButton();
    initValidator();
    dismissEvent();
    ajaxVerifyUserInfo();
});

/**
* 添加新用户，编辑用户时，用ajax动态验证用户名，域名，旅行社是否可用
*/
function ajaxVerifyUserInfo(){
        //由于ajax是异步提交，所以必须放在外面
        var flag = true;
    //是否阻止表单提交
    $('form').submit(function(){
        var url;
        var $form = $(this);
        var loginname = $form.find('.username').val();
        var domain = $form.find('.domain').val();
        var travel = $form.find('.travel').val();
        var adminId = $form.find('.adminId').val();
        var data = 
            {'loginname':loginname, 'domain':domain, 'travel': travel, 'adminId': adminId};

        if($form.attr('id') == 'add-form'){
            url = ajaxVerifyAddUrl;
        }else if($form.attr('id') == 'edit-form') {
            url = ajaxVerifyEditUrl;
        } 
        $.post(url, data, function(response, status){
                    response = eval(response);
                    var respStatus = response.status;
                    if(respStatus == false){
                        $form.find('.error-tips').html(response.info).show();
                        setTimeout(function(){
                        $form.find('.error-tips').hide(); 
                    }, 5000);
                 }else{
                    flag = false;
                    $form.submit();
                 }
            });
        // $form.find('.submit').attr('disabled', '');
        if(flag){
            return false;
        }     
    });
}
/**
* 模态框关闭后，清楚模态框的内容
*/
function dismissEvent(){
    $('#add-form').data('bootstrapValidator').resetForm(true);
    $('#edit-form').data('bootstrapValidator').resetForm(true);

    $('#add-administrator').on('hidden.bs.modal', function (e) {
        $('#add-form').data('bootstrapValidator').resetForm(true);
    })
    $('#edit-administrator').on('hidden.bs.modal', function (e) {
        $('#edit-form').data('bootstrapValidator').resetForm(true);
    })
}
//编辑按钮,点击改变编辑模态框提交的表单
function editButton(){
    $(".update").each(function(){
        $(this).click(function(){
            var id= $(this).attr("id");
            $("#idInput").val(id);
           // alert($("#idInput").val());
        });
    });
}

/**
* 表单验证
*/
function initValidator(){
        $('#add-form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                "admin[loginname]": {
                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        }
                    }
                },
                "admin[password]": {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        different: {
                            field: 'admin[loginname]',
                            message: '密码不能与用户名相同'
                        }
                    }
                },
                repassword: {
                    validators: {
                        notEmpty: {
                            message: '重复密码不能为空'
                        },
                        identical: {
                            field: 'admin[password]',
                            message: '重复密码与密码不一样'
                        }
                    }
                },
                "admin[tvl_name]": {
                    validators: {
                        notEmpty: {
                            message: '旅行社不能为空'
                        }
                    }
                },
                "admin[tvl_domain]": {
                    validators: {
                        notEmpty: {
                            message: "域名不能为空"
                        }
                    }
                },
                "admin[tvl_level]": {
                    validators: {
                        notEmpty: {
                            message: '等级不能为空'
                        }
                    }
                }

            }
        });
        $('#edit-form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                "admin[password]": {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }
                },
                repassword: {
                    validators: {
                        notEmpty: {
                            message: '重复密码不能为空'
                        },
                        identical: {
                            field: 'admin[password]',
                            message: '重复密码与密码不一样'
                        }
                    }
                },
                "admin[tvl_name]": {
                    validators: {
                        notEmpty: {
                            message: '旅行社不能为空'
                        }
                    }
                },
                "admin[tvl_domain]": {
                    validators: {
                        notEmpty: {
                            message: "域名不能为空"
                        }
                    }
                },
                "admin[tvl_level]": {
                    validators: {
                        notEmpty: {
                            message: '等级不能为空'
                        }
                    }
                }
            }
        });
        $('#add-reset').click(function() {
            $('#add-form').data('bootstrapValidator').resetForm(true);
        });

        $('#edit-reset').click(function() {
            $('#edit-form').data('bootstrapValidator').resetForm(true);
        });
}




